package com.bt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bt.annotation.RequiresPermissionsDesc;
import com.bt.pojo.DtsCategory;
import com.bt.service.DtsAdminService;
import com.bt.service.DtsCategoryService;
import com.bt.util.ResponseUtil;
import com.bt.vo.CatVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wbt
 * @version v1.0
 * @project shop_manage
 * @data 2022/9/24 20:39
 **/
@RestController
@RequestMapping("/admin/category")
@CrossOrigin
public class AdminCategoryController {
    @Autowired
    private DtsCategoryService dtsCategoryService;
    /**
     * http://localhost:8080/admin/category/list?page=1&limit=20&id=11&name=22&sort=add_time&order=desc
     * get
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:category:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "商场管理"}, button = "查询商品类目")
    public Object list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer limit, Integer id, String name, String sort, String order){
        if (page<1){
            page=1;
        }
        if (limit<1){
            limit=20;
        }
        IPage<DtsCategory> Ipage=dtsCategoryService.findCategoryByPage(page,limit,id,name,sort,order);
        Map data=new HashMap<>();
        data.put("tatal",Ipage.getTotal());
        data.put("items",Ipage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     * http://localhost:8080/admin/category/l1
     * get
     */
    @GetMapping("/l1")
    @RequiresPermissions("admin:category:l1")
    @RequiresPermissionsDesc(menu = {"系统管理", "商场管理"}, button = "查询一级类目")
    public Object l1(){
        List<CatVo> catVoList=dtsCategoryService.findL1CatVos();
        return ResponseUtil.ok(catVoList);
    }
    /**
     * http://localhost:8080/admin/category/update
     * post
     */
    @PostMapping("/update")
    @RequiresPermissions("admin:category:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "商场管理"}, button = "更新商品类目")
    public Object update(@RequestBody DtsCategory dtsCategory){
        try {
            dtsCategoryService.updateDtsCategory(dtsCategory);
            return ResponseUtil.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail();
        }
    }
    /**
     * http://localhost:8080/admin/category/create
     * post
     */
    @PostMapping("/create")
    @RequiresPermissions("admin:category:create")
    @RequiresPermissionsDesc(menu = {"系统管理", "商场管理"}, button = "创建商品类目")
    public Object create(@RequestBody DtsCategory dtsCategory){
        try {
            dtsCategoryService.createDtsCategory(dtsCategory);
            return ResponseUtil.ok(dtsCategory);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail();
        }
    }
    /**
     * http://localhost:8080/admin/category/delete
     * post
     */
    @PostMapping("/delete")
    @RequiresPermissions("admin:category:delete")
    @RequiresPermissionsDesc(menu = {"系统管理", "商场管理"}, button = "删除商品类目")
    public Object delete(@RequestBody DtsCategory dtsCategory){
        if (dtsCategory.getId()==null){
            return ResponseUtil.fail();
        }
        try {
            dtsCategoryService.deleteDtsCategory(dtsCategory);
            return ResponseUtil.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail();
        }
    }

}
